package com.swallow.auth.infrastructure.mysql.po;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 账号表
 * </p>
 *
 * @author 18310740596@163.com
 * @since 2024-05-11
 */
@Getter
@Setter
@TableName("swallow_auth_account")
public class AccountPO implements UserDetails, Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 自增主键
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 账户唯一编号
     */
    private String accountNo;

    /**
     * 手机号
     */
    private String mobile;

    /**
     * 账号名称
     */
    private String accountName;

    private String accountPassword;

    /**
     * 账号注册时间
     */
    private LocalDateTime registerTime;

    /**
     * 账号状态  1-有效 2-失效
     */
    private Integer accountStatus;

    /**
     * 登录错误次数
     */
    private Integer loginErrorCount;

    /**
     * 登录时间
     */
    private LocalDateTime loginTime;

    /**
     * 账号锁定状态 0-未锁定 1-锁定
     */
    private Integer accountLockStatus;

    /**
     * 账号解锁时间
     */
    private LocalDateTime accountUnlockTime;

    @TableField(exist = false)
    private String tenantNo;

    @TableField(exist = false)
    private String tenantName;
    /**
     *  员工名称
     */
    @TableField(exist = false)
    private String staffName;

    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    /**
     * 删除标识  0-未删除  其他-已删除
     */
    @TableLogic
    private Long deleteFlag;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return List.of();
    }

    @Override
    public String getPassword() {
        return getAccountPassword();
    }

    @Override
    public String getUsername() {
        return getAccountNo();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return Objects.isNull(getAccountLockStatus()) || getAccountLockStatus() == 0;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return getDeleteFlag() == 0;
    }
}
